<template>
  <div>
    <el-card class="box-card" v-if="this.cartItems.length">
      <div slot="header" class="clearfix" style="text-align: left;">
        <span>购物车</span>
      </div>
      <el-table
        :data="cartItems"
        border
        style="width: 100%; margin-top: 20px">
        <el-table-column
          prop="barcode"
          label="商品条码">
        </el-table-column>
        <el-table-column
          prop="name"
          label="商品名称">
        </el-table-column>
        <el-table-column
          label="商品价格">
          <template slot-scope="scope">
            {{ scope.row.price + ' 元' }}
          </template>
        </el-table-column>
        <el-table-column
          label="商品数量">
          <template slot-scope="scope">
            {{ scope.row.quantity + ' (' + scope.row.unit + ')' }}
          </template>
        </el-table-column>
        <el-table-column
          label="商品总价">
          <template slot-scope="scope">
            {{ scope.row.price * scope.row.quantity + ' 元' }}
          </template>
        </el-table-column>
      </el-table>
      <div>合计：{{ totalAmount + ' 元' }}</div>
    </el-card>
    <el-card class="box-card">
      <div slot="header" class="clearfix" style="text-align: left;">
        <span>扫码商品</span>
      </div>
      <el-form :inline="true" :model="addCartForm" class="demo-form-inline">
        <el-form-item label="商品条码">
          <el-input v-model="addCartForm.barcode" placeholder="请输入条码"></el-input>
        </el-form-item>
        <el-form-item label="商品数量">
          <el-input v-model="addCartForm.quantity" placeholder="请输入商品数量"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addCart">加入</el-button>
          <el-button type="success" @click="pay">付款完成</el-button>
          <el-button type="danger" @click="clearCart">清空购物车</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import shoppingCartService from '@/global/service/shoppingCartService';
import orderService from '@/global/service/orderService';

export default {
  name: 'Cashier',
  data() {
    return {
      addCartForm: {
        barcode: '',
        quantity: 1,
      },
      cartItems: [],
      totalAmount: 0,
    }
  },
  created() {
    this.getCart();
  },
  methods: {
    addCart() {
      shoppingCartService.add({
        barcode: this.addCartForm.barcode,
        quantity: this.addCartForm.quantity,
      }).then(() => {
        this.getCart();
        this.$message.success('加入成功!')
      })
    },
    getCart() {
      shoppingCartService.getCart().then((res) => {
        this.cartItems = res.cartItems;
        this.totalAmount = res.totalAmount;
        this.addCartForm.barcode = ''
      })
    },
    clearCart() {
      shoppingCartService.clearCart().then(() => {
        this.cartItems = [];
        this.totalAmount = 0;
      })
    },
    pay() {
      orderService.pay().then(() => {
        this.getCart()
        this.$message.success('完成付款!')
      })
    },
  },
}
</script>
